﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.IO;

namespace qianjing.APP
{
    public class SendSms
    {

        public string SendMsg(string mobile1, string msg)
        {
            string PostUrl = "http://222.73.117.156/msg/HttpBatchSendSM";
            string res = "";
            string account = "qianjingjiating";
            string password = "Aa12345.";
            string mobile = mobile1;
            string content = msg;

            string postStrTpl = "account={0}&pswd={1}&mobile={2}&msg={3}&needstatus=true&extno=";

            UTF8Encoding encoding = new UTF8Encoding();
            byte[] postData = encoding.GetBytes(string.Format(postStrTpl, account, password, mobile, content));

            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(PostUrl);
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = postData.Length;

            Stream newStream = myRequest.GetRequestStream();
            // Send the data.
            newStream.Write(postData, 0, postData.Length);
            newStream.Flush();
            newStream.Close();

            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            if (myResponse.StatusCode == HttpStatusCode.OK)
            {
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
               
                res = reader.ReadToEnd();

                //反序列化upfileMmsMsg.Text
                //实现自己的逻辑
            }
            else
            {
                //访问失败
            }

            return res;
        }

        /// 

        /// 以 HTTP 的 POST 提交方式 发送短信(ASP.NET的网页或是C#的窗体，均可使用该方法)
        /// 

        /// 要发送的手机号码
        /// 要发送的内容
        /// 发送结果
        //public string SendMsg(string mobile, string msg)
        //{
        //    string name = "17751289961";
        //    string pwd = "7EF141B73D4188F4123D859D7CA1";//登陆web平台 http://sms.1xinxi.cn  在管理中心--基本资料--接口密码（28位） 如登陆密码修改，接口密码会发生改变，请及时修改程序
        //    string sign = "千净家政";             //一般为企业简称
        //    StringBuilder arge = new StringBuilder();

        //    arge.AppendFormat("name={0}", name);
        //    arge.AppendFormat("&pwd={0}", pwd);
        //    arge.AppendFormat("&content={0}", msg);
        //    arge.AppendFormat("&mobile={0}", mobile);
        //    arge.AppendFormat("&sign={0}", sign);
        //    arge.Append("&type=pt");
        //    string weburl = "http://sms.1xinxi.cn/asmx/smsservice.aspx";

        //    string resp = PushToWeb(weburl, arge.ToString(), Encoding.UTF8);
        //    if (resp.Split(',')[0] == "0")
        //    {
        //        //提交成功
        //    }
        //    else
        //    {
        //        //提交失败，可能余额不足，或者敏感词汇等等
        //    }

        //    return resp;//是一串 以逗号隔开的字符串。阅读文档查看响应的意思
        //}

        /// 

        /// HTTP POST方式
        /// 

        /// POST到的网址
        /// POST的参数及参数值
        /// 编码方式
        /// 
        public string PushToWeb(string weburl, string data, Encoding encode)
        {
            byte[] byteArray = encode.GetBytes(data);

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(weburl));
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = byteArray.Length;
            Stream newStream = webRequest.GetRequestStream();
            newStream.Write(byteArray, 0, byteArray.Length);
            newStream.Close();

            //接收返回信息：
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            StreamReader aspx = new StreamReader(response.GetResponseStream(), encode);
            return aspx.ReadToEnd();
        }
    }
}